<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Layui</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta HTTP-EQUIV="pragma" CONTENT="no-cache"> 
        <meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
        <meta HTTP-EQUIV="expires" CONTENT="0">
        <link rel="stylesheet" href="../../../js/layui/css/layui.css" media="all">
        <style type="text/css">
            #upfile{
                width: auto;
            }
            #img{
                width: 100%;
                height: 100%;
                vertical-align: middle;
                horiz-align: center
            }
        </style>
    </head>
    <body>
    <div class="layui-col-md5">
        <form class="layui-form" id="addForm" action="">
            <div class="layui-form-item">
                <div class="layui-inline" style="display: none">
                    <label class="layui-form-label">productId：</label>
                    <div class="layui-input-inline">
                        <input type="tel" id="productId" name="phone" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">产品名：</label>
                    <div class="layui-input-inline">
                        <input type="tel" id="productName" name="phone" required lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">价格：</label>
                    <div class="layui-input-inline">
                        <input type="tel" id="price" name="phone" required lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">份数：</label>
                    <div class="layui-input-inline">
                        <input type="tel" id="num" name="phone" required lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">顺序：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="sort" name="email" lay-verify="number" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">描述：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="description" name="email" lay-verify="" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="demo1" src="" >
                        <p id="demoText"></p>
                    </div>

                </div>
            </div>
        </form>
    </div>
    <div class="layui-col-md7" style="background-color: #00FF00">

    </div>

    <script src="../../../js/layui/layui.js"></script>
    <script src="../../../js/myJQ.js"></script>
    <script>
        layui.use(['upload','jquery','layer'], function() {
            var basePath = getRootPath();
            var $ = layui.$;
            var upload = layui.upload;
            var layer = layui.layer;
            $(function () {
                debugger
                var para= RequestParameter();
                if(!$.isEmptyObject(para)){
                    getProduct(para.productId);
                }
            });

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                ,url: basePath+'/product/upImg'
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                ,accept:"images"
                ,done: function(res){
                    //如果上传失败
                    if(res.code > 0){
                        return layer.msg('上传失败');
                    }
                    //上传成功
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });

            function getProduct (id) {
                debugger
                var param = {};
                param.productId=id;
                $.ajax({
                    url:basePath+"/product/getProductList",
                    dataType:"json",
                    async:false,
                    type:"POST",
                    data:param,
                    cache:false,
                    beforeSend :function(xmlHttp){
                        xmlHttp.setRequestHeader("If-Modified-Since","0");  //添加
                        xmlHttp.setRequestHeader("Cache-Control","no-cache"); //添加
                    },
                    success:function (result) {
                        debugger
                        $("#productId").val(result.data[0].productId);
                        $("#productName").val(result.data[0].productName);
                        $("#price").val(result.data[0].price);
                        $("#num").val(result.data[0].num);
                        $("#sort").val(result.data[0].sort);
                        $("#description").val(result.data[0].description);
                        $("#demo1").attr('src',"../"+result.data[0].img);
                    }
                });
            }

            /**
             * 如果是编辑，获取该数据的ID
             * @returns {Object}
             * @constructor
             */
            function RequestParameter(){
                var url = window.location.search; //获取url中"?"符后的字串
                var theRequest = new Object();
                if (url.indexOf("?") != -1) {
                    var str = url.substr(1);
                    var strs = str.split("&");
                    for(var i = 0; i < strs.length; i ++) {
                        theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
                    }
                }
                return theRequest;
            }
        });
    </script>
    </body>
</html>